home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / second / RCS / second.c,v < prev   
Encoding:
Text File  |  1992-07-17  |  4.2 KB  |  233 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.3 srv027:1.3 srv026:1.3 srv024:1.3 srv021:1.3 srv018:1.3 srv014:1.3 srv010:1.3 srv008:1.3 srv007:1.3 srv006:1.3 srv004:1.3;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     92.01.22.13.29.33;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     91.11.14.20.27.09;  author kupfer;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     91.10.04.12.24.53;  author kupfer;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @Snapshot.  Prints its arguments and cat's any named files.
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @Use stat() instead of TempFs_GetLength().
  33. @
  34. text
  35. @/* Quick test program to get exec'd. */
  36.  
  37. #include <sprite.h>
  38. #include <cfuncproto.h>
  39. #include <errno.h>
  40. #include <mach.h>
  41. #include <status.h>
  42. #include <string.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <test.h>
  46. #include <vm.h>
  47.  
  48. static void CatFile();
  49. static int GetLength _ARGS_((char *fileName));
  50. static void MapFile _ARGS_((char *fileName, boolean_t readOnly,
  51.                 int length, Address *startAddrPtr));
  52.  
  53. int
  54. main(argc, argv)
  55.     int argc;
  56.     char *argv[];
  57. {
  58.     int i;
  59.  
  60.     /* print the arguments */
  61.     Test_PutDecimal(argc);
  62.     Test_PutMessage(" arguments: \n");
  63.     for (i = 0; i < argc; i++) {
  64.     Test_PutMessage(argv[i]);
  65.     Test_PutMessage("\n");
  66.     }
  67.  
  68.     /* if there was a file named, cat it */
  69.     for (i = 1; i < argc; i++) {
  70.     if (argv[i][0] == '-') {
  71.         continue;
  72.     }
  73.     CatFile(argv[i]);
  74.     }
  75.  
  76.     return 0;
  77. }
  78.  
  79.  
  80. /*
  81.  *----------------------------------------------------------------------
  82.  *
  83.  * CatFile --
  84.  *
  85.  *    Print the contents of a file.
  86.  *
  87.  * Results:
  88.  *    None.
  89.  *
  90.  * Side effects:
  91.  *    None.
  92.  *
  93.  *----------------------------------------------------------------------
  94.  */
  95.  
  96. static void
  97. CatFile(fileName)
  98.     char *fileName;
  99. {
  100.     int fileLength;
  101.     char *buffer;
  102.  
  103.     Test_PutMessage("----- ");
  104.     Test_PutMessage(fileName);
  105.     Test_PutMessage(" -----\n");
  106.  
  107.     if (strcmp(fileName, "killMe") == 0) {
  108.     *(char *)0xffffffff = fileName[0];
  109.     }
  110.     if (strcmp(fileName, "nullArg") == 0) {
  111.     fileName = (char *)0;
  112.     }
  113.     fileLength = GetLength(fileName);
  114.     if (fileLength > 0) {
  115.     MapFile(fileName, TRUE, fileLength, &buffer);
  116.     if (buffer != 0) {
  117.         Test_PutString(buffer, fileLength);
  118.         vm_deallocate(mach_task_self(), (vm_address_t)buffer, fileLength);
  119.     }
  120.     }
  121. }
  122.  
  123.  
  124. /*
  125.  *----------------------------------------------------------------------
  126.  *
  127.  * GetLength --
  128.  *
  129.  *    Get the length of a file.
  130.  *
  131.  * Results:
  132.  *    Returns the length of the file, in bytes.  Returns -1 if there 
  133.  *    was an error.
  134.  *
  135.  * Side effects:
  136.  *    None.
  137.  *
  138.  *----------------------------------------------------------------------
  139.  */
  140.  
  141. static int
  142. GetLength(fileName)
  143.     char *fileName;
  144. {
  145.     struct stat statBuf;
  146.  
  147.     if (stat(fileName, &statBuf) < 0) {
  148.     Test_PutMessage("Couldn't get length of `");
  149.     Test_PutMessage(fileName);
  150.     Test_PutMessage("': ");
  151.     Test_PutMessage(strerror(errno));
  152.     Test_PutMessage("\n");
  153.     return -1;
  154.     }
  155.  
  156.     return statBuf.st_size;
  157. }
  158.  
  159.  
  160. /*
  161.  *----------------------------------------------------------------------
  162.  *
  163.  * MapFile --
  164.  *
  165.  *    Map the named file into our address space.
  166.  *
  167.  * Results:
  168.  *    Fills in the starting location, which is set to 0 
  169.  *    if there was a problem.
  170.  *
  171.  * Side effects:
  172.  *    None.
  173.  *
  174.  *----------------------------------------------------------------------
  175.  */
  176.  
  177. static void
  178. MapFile(fileName, readOnly, length, startAddrPtr)
  179.     char *fileName;        /* name of file to map */
  180.     Boolean readOnly;        /* map read-only or read-write? */
  181.     int length;            /* number of bytes to map */
  182.     Address *startAddrPtr;    /* OUT: where the file was mapped to */
  183. {
  184.     ReturnStatus status;
  185.  
  186.     status = Vm_MapFile(fileName, readOnly, 0, length, startAddrPtr);
  187.     if (status != SUCCESS) {
  188.     Test_PutMessage("Couldn't map `");
  189.     Test_PutMessage(fileName);
  190.     Test_PutMessage("': ");
  191.     Test_PutMessage(Stat_GetMsg(status));
  192.     Test_PutMessage("\n");
  193.     *startAddrPtr = 0;
  194.     }
  195. }
  196.  
  197. @
  198.  
  199.  
  200. 1.2
  201. log
  202. @Add "nullArg" backdoor (for testing).
  203. @
  204. text
  205. @d5 1
  206. d9 2
  207. a10 1
  208. #include <tempFs.h>
  209. d111 1
  210. a111 2
  211.     ReturnStatus status;
  212.     int length;
  213. d113 1
  214. a113 2
  215.     status = TempFs_Length(fileName, &length);
  216.     if (status != SUCCESS) {
  217. d117 1
  218. a117 1
  219.     Test_PutMessage(Stat_GetMsg(status));
  220. d122 1
  221. a122 1
  222.     return length;
  223. @
  224.  
  225.  
  226. 1.1
  227. log
  228. @Initial revision
  229. @
  230. text
  231. @d74 3
  232. @
  233.